using System.IO;
using FireStarter.Base;
using FireStarter.CodeGeneration.Generator;
using FireStarterModeller.BO;

namespace FireStarter.CodeGeneration
{
    public class GenerationUtilities
    {
        public static void GenerateCodeFile(ProjectFileManager projectFileManager, string templateFileName, string projectPath, string codeFileName, IModelElement modelElement)
        {
            string errorMessage;
            TemplateExpression templateExpression = 
                TemplateUtilities.GetTemplateFromFile(templateFileName, out errorMessage);
            if (templateExpression != null)
            {
                ModelStrategyMapper modelStrategyMapper = new ModelStrategyMapper();
                modelStrategyMapper.SetStrategy(typeof(IModelElement), new ReflectionGenerationStrategy());
                string generatedContents = templateExpression.Generate(modelStrategyMapper, modelElement);
                string fileName = Path.Combine(projectPath, codeFileName);
                Utilities.CreateFile(fileName, generatedContents, false);
                if (!projectFileManager.HasCodeFile(fileName))
                {
                    projectFileManager.AddCodeFile(fileName);
                }
            }
        }

        public static string GetProjectName(string projectFileName)
        {
            return Path.GetFileNameWithoutExtension(projectFileName);
        }
    }
}